<template>
    <!-- 总容器 -->
    <div class="wrapper">
        <header>
            <i class="fa fa-angle-left" onclick="history.go(-1)"></i>
            <p>确认您的订单</p>
            <div></div>
        </header>
        <div class="top-ban"></div>

        <section>
            <div class="title">
                <p>体检人信息</p>
            </div>
            <table>
                <tbody>
                    <tr>
                        <td>姓名:</td>
                        <td>{{ user.realname }}</td>
                    </tr>
                    <tr>
                        <td>证件号码:</td>
                        <td>{{ user.identitycard }}</td>
                    </tr>
                    <tr>
                        <td>出生日期:</td>
                        <td>{{ user.birthday }}</td>
                    </tr>
                    <tr>
                        <td>手机号码:</td>
                        <td>{{ user.userid }}</td>
                    </tr>
                </tbody>
            </table>
            <div class="title">
                <p>体检日期</p>
            </div>
            <table>
                <tbody>
                    <tr>
                        <td>{{ date }}</td>
                    </tr>
                </tbody>
            </table>
            <div class="title">
                <p>体检机构</p>
            </div>
            <table>
                <tbody>
                    <tr>
                        <td colspan="2">{{ hospital.name }}</td>
                    </tr>
                    <tr>
                        <td>营业时间:</td>
                        <td>{{ hospital.businesshours }}</td>
                    </tr>
                    <tr>
                        <td>采血截止:</td>
                        <td>{{ hospital.deadline }}</td>
                    </tr>
                    <tr>
                        <td>机构电话:</td>
                        <td>{{ hospital.telephone }}</td>
                    </tr>
                    <tr>
                        <td>机构地址:</td>
                        <td>{{ hospital.address }}</td>
                    </tr>
                </tbody>
            </table>
            <div class="title">
                <p>套餐类型</p>
            </div>
            <table>
                <tbody>
                    <tr>
                        <td>普通{{ setmeal.name }}</td>
                    </tr>
                </tbody>
            </table>
        </section>
        <div ref="alipayForm"></div>
        <div class="bottom-btn">
            <div class="first">
                实付款: <span>￥{{ setmeal.price }}</span>
            </div>
            <div class="last" @click="gopay">确认支付</div>
        </div>

        <div class="bottom-ban"></div>
        <tijianfooter></tijianfooter>
    </div>
</template>

<script setup>
import "../style/css/confirmorder.css";
import tijianfooter from "../components/tijianfooter.vue";

import {getSessionStorage} from "../utils/common";
import {useRoute, useRouter} from "vue-router";
import {reactive, ref} from "vue";
import axios from "axios";

import {onMounted} from "vue";

const user = getSessionStorage("user");
const router = useRouter();
const route = useRoute();
const hospital = ref({});
const setmeal = ref({});
const date = route.query.date;
const alipayForm = ref();

onMounted(async () => {
    console.log(user);
    await axios.post("/api/hospital/getbyHpId", {hpid: route.query.hospitalId}).then((res) => {
        hospital.value = res.data.data;
    });
    await axios.post("/api/setmeal/getSetmealdetail", {smid: route.query.setmealId}).then((res) => {
        setmeal.value = res.data.data;
    });
});
const gopay = () => {
    try {
        axios.put("/api/order/confirmOrder", {
            userid: user.userid,
            hpid: route.query.hospitalId,
            orderdate: date,
            smid: route.query.setmealId,
            state: 1,
        }).then((res) => {
            const order = res.data.data;
            
            if (res.data.status === 200) {
                axios.get('/api/pay/alipay',{params: {orderid:order.orderid}}).then((response) => {
                    console.log(response);
                    // 将form表单字符串添加到<div>中
                    alipayForm.value.innerHTML = response.data;
                    
                    window.document.forms[0].submit();
                })
                .catch((error) => {
                    console.log(error);
                });
            }
        });
    } catch (error) {
        console.log(error);
    }
};
</script>
